package com.haoqizhe.kernel.activemq.connection;

import com.haoqizhe.kernel.activemq.properties.ActivemqProperties;
import com.haoqizhe.kernel.activemq.properties.ConsumerProperties;
import org.apache.activemq.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;


/**
 *
 * 配置注册链接工厂
 *
 * @author  haoqizhe.li on 2017/6/12.
 */
@Configuration
@EnableConfigurationProperties({ActiveMQProperties.class, ConsumerProperties.class, ActivemqProperties.class})
public class ActiveMQXAConnectionFactoryConfiguration {

    @Autowired
    private ConsumerProperties consumerProperties;

    @Autowired
    private ActivemqProperties activemqProperties;

    /**
     * 创建普通链接工厂
    * @Param properties
    * @returnType 
    * @Author haoqizhe.li
    * @Date 2017/6/12 22:17
    **/
    @Bean(name="jmsConnectionFactory")
    public ActiveMQConnectionFactory nonXaJmsConnectionFactory(ActiveMQProperties activeMQProperties){
        ActiveMQConnectionFactory activeMQConnectionFactory =  new ActiveMQConnectionFactoryFactory(activeMQProperties,activemqProperties)
                .createConnectionFactory(ActiveMQConnectionFactory.class);

        /**
         * 设置连接broker url options
         */
        if(activemqProperties != null){
            activeMQConnectionFactory.setAlwaysSessionAsync(activemqProperties.isAlwaysSessionAsync());
            activeMQConnectionFactory.setAlwaysSyncSend(activemqProperties.isAlwaysSyncSend());
            activeMQConnectionFactory.setAuditDepth(activemqProperties.getAuditDepth());
            activeMQConnectionFactory.setAuditMaximumProducerNumber(activemqProperties.getAuditMaximumProducerNumber());
            activeMQConnectionFactory.setCheckForDuplicates(activemqProperties.isCheckForDuplicates());
            activeMQConnectionFactory.setCloseTimeout(activemqProperties.getCloseTimeout());
            activeMQConnectionFactory.setConnectResponseTimeout(activemqProperties.getConnectResponseTimeout());
            activeMQConnectionFactory.setConsumerExpiryCheckEnabled(activemqProperties.isConsumerExpiryCheckEnabled());
            activeMQConnectionFactory.setConsumerFailoverRedeliveryWaitPeriod(activemqProperties.getConsumerFailoverRedeliveryWaitPeriod());
            activeMQConnectionFactory.setCopyMessageOnSend(activemqProperties.isCopyMessageOnSend());
            activeMQConnectionFactory.setDisableTimeStampsByDefault(activemqProperties.isDisableTimeStampsByDefault());
            activeMQConnectionFactory.setDispatchAsync(activemqProperties.isDispatchAsync());
            activeMQConnectionFactory.setMaxThreadPoolSize(activemqProperties.getMaxThreadPoolSize());
            activeMQConnectionFactory.setMessagePrioritySupported(activemqProperties.isMessagePrioritySupported());
            activeMQConnectionFactory.setNestedMapAndListEnabled(activemqProperties.isNestedMapAndListEnabled());
            activeMQConnectionFactory.setNonBlockingRedelivery(activemqProperties.isNonBlockingRedelivery());
            activeMQConnectionFactory.setOptimizedAckScheduledAckInterval(activemqProperties.getOptimizedAckScheduledAckInterval());
            activeMQConnectionFactory.setOptimizedMessageDispatch(activemqProperties.isOptimizedMessageDispatch());
            activeMQConnectionFactory.setProducerWindowSize(activemqProperties.getProducerWindowSize());
            activeMQConnectionFactory.setRmIdFromConnectionId(activemqProperties.isRmIdFromConnectionId());
            activeMQConnectionFactory.setSendAcksAsync(activemqProperties.isSendAcksAsync());
            activeMQConnectionFactory.setSendTimeout(activemqProperties.getSendTimeout());
            activeMQConnectionFactory.setTransactedIndividualAck(activemqProperties.isTransactedIndividualAck());
            activeMQConnectionFactory.setUseAsyncSend(activemqProperties.isUseAsyncSend());
            activeMQConnectionFactory.setUseCompression(activemqProperties.isUseCompression());
            activeMQConnectionFactory.setWarnAboutUnstartedConnectionTimeout(activemqProperties.getWarnAboutUnstartedConnectionTimeout());
            activeMQConnectionFactory.setWatchTopicAdvisories(activemqProperties.isWatchTopicAdvisories());

            if(activemqProperties.isExclusiveConsumer() != null){
                activeMQConnectionFactory.setExclusiveConsumer(activemqProperties.isExclusiveConsumer());
            }
            if(activemqProperties.isObjectMessageSerializationDefered() != null){
                activeMQConnectionFactory.setObjectMessageSerializationDefered(activemqProperties.isObjectMessageSerializationDefered().booleanValue());
            }
            if(activemqProperties.isOptimizeAcknowledge() != null){
                activeMQConnectionFactory.setOptimizeAcknowledge(activemqProperties.isOptimizeAcknowledge().booleanValue());
            }
            if(activemqProperties.isUseDedicatedTaskRunner() != null){
                activeMQConnectionFactory.setUseDedicatedTaskRunner(activemqProperties.isUseDedicatedTaskRunner().booleanValue());
            }
            if(activemqProperties.isUseRetroactiveConsumer() != null){
                activeMQConnectionFactory.setUseRetroactiveConsumer(activemqProperties.isUseRetroactiveConsumer().booleanValue());
            }
            if(activemqProperties.isStatsEnabled() != null ){
                activeMQConnectionFactory.setStatsEnabled(activemqProperties.isStatsEnabled().booleanValue());
            }
        }

        /**
         * 设置消息重发策略
         */
        RedeliveryPolicy redeliveryPolicy =  activeMQConnectionFactory.getRedeliveryPolicy();

        if(consumerProperties != null && consumerProperties.getRedeliveryPolicy() != null){
            redeliveryPolicy.setMaximumRedeliveries(consumerProperties.getRedeliveryPolicy().getMaximumRedeliveries());
            redeliveryPolicy.setMaximumRedeliveryDelay(consumerProperties.getRedeliveryPolicy().getMaximumRedeliveryDelay());
            redeliveryPolicy.setInitialRedeliveryDelay(consumerProperties.getRedeliveryPolicy().getInitialRedeliveryDelay());
            redeliveryPolicy.setRedeliveryDelay(consumerProperties.getRedeliveryPolicy().getRedeliveryDelay());
            redeliveryPolicy.setBackOffMultiplier(consumerProperties.getRedeliveryPolicy().getBackOffMultiplier());
        }
        /**
         * 设置消息预取策略
         */
        ActiveMQPrefetchPolicy  prefetchPolicy =  activeMQConnectionFactory.getPrefetchPolicy();
        if(consumerProperties != null && consumerProperties.getPrefetchPolicy() != null){

            if(consumerProperties.getPrefetchPolicy().getAll() > 0){
                prefetchPolicy.setAll(consumerProperties.getPrefetchPolicy().getAll());
            }else{
                prefetchPolicy.setQueuePrefetch(consumerProperties.getPrefetchPolicy().getQueuePrefetch());
                prefetchPolicy.setTopicPrefetch(consumerProperties.getPrefetchPolicy().getTopicPrefetch());
                prefetchPolicy.setDurableTopicPrefetch(consumerProperties.getPrefetchPolicy().getDurableTopicPrefetch());
                prefetchPolicy.setQueueBrowserPrefetch(consumerProperties.getPrefetchPolicy().getQueueBrowserPrefetch());
                prefetchPolicy.setOptimizeDurableTopicPrefetch(consumerProperties.getPrefetchPolicy().getOptimizeDurableTopicPrefetch());
            }
            if(consumerProperties.getPrefetchPolicy().getMaximumPendingMessageLimit() > 0){
                prefetchPolicy.setMaximumPendingMessageLimit(consumerProperties.getPrefetchPolicy().getMaximumPendingMessageLimit());
            }

        }

        return activeMQConnectionFactory;
    }

}
